
#include "HandleRequest.h"
#include "Virtual/ClipBoard.h"
#include "Virtual/VSerives.h"

static CHandleProcess gHandleProcess;

CHandleProcess::CHandleProcess()
{
    m_ProcessRequest[BoxMessage_Start] = CHandleProcess::ProcessStart;
    m_ProcessRequest[BoxMessage_ConnectAccess] = CHandleProcess::ConnectAccess;
    m_ProcessRequest[BoxMessage_VirtualClipBoard] = CHandleProcess::VirtualClipBoard;
    m_ProcessRequest[BoxMessage_VirtualServices] = CHandleProcess::VirtualServices;
    m_ProcessRequest[BoxMessage_VirtualServiceControl] = CHandleProcess::VirtualServiceControl;
}

CHandleProcess::~CHandleProcess()
{

}

CHandleProcess& CHandleProcess::GetInstance()
{
    return gHandleProcess;
}

BOOL CHandleProcess::DispathRequest(BoxMessage uMessage, 
    HIOCPFILE hObject, PNET_BUFFER Buffer)
{
    BOOL bRet = FALSE;
    auto it = m_ProcessRequest.find(uMessage);
    if (it != m_ProcessRequest.end())
    {
        it->second(hObject, Buffer->Buffer, Buffer->Offset);
        bRet = TRUE;
    }
    return bRet;
}

void CHandleProcess::ProcessStart(HIOCPFILE hObject, PVOID Buffer, ULONG Size)
{
    PPROCESS_START_REQ pData = (PPROCESS_START_REQ)Buffer;
    PROCESS_START_RPL Response = { 0 };
    Response.Base.Lenght = sizeof(PROCESS_START_RPL);
    Response.Base.ThreadId = pData->Base.ThreadId;
    Response.Base.Message = pData->Base.Message;
    Response.Base.Sequence = pData->Base.Sequence;

    Response.nCode = 0;

    int ret = fileio_write(hObject, (const char*)&Response, Response.Base.Lenght);
}

void CHandleProcess::ConnectAccess(HIOCPFILE hObject, PVOID Buffer, ULONG Size)
{
    PCONNECT_ACCESS_REQ pData = (PCONNECT_ACCESS_REQ)Buffer;
    CONNECT_ACCESS_RPL Response = { 0 };
    Response.Base.Lenght = sizeof(CONNECT_ACCESS_RPL);
    Response.Base.ThreadId = pData->Base.ThreadId;
    Response.Base.Message = pData->Base.Message;
    Response.Base.Sequence = pData->Base.Sequence;

    Response.dwFlags = 0;

    int ret = fileio_write(hObject, (const char*)&Response, Response.Base.Lenght);
}

void CHandleProcess::VirtualClipBoard(HIOCPFILE hObject, PVOID Buffer, ULONG Size)
{
    ::ClipBoard_DispatchHandle(hObject, (PVIRTUAL_CLIPBOARD_REQ)Buffer);
}

void CHandleProcess::VirtualServices(HIOCPFILE hObject, PVOID Buffer, ULONG Size)
{
    ::Services_DispatchHandle(hObject, (PVIRTUAL_SERVICE_LPCBASE)Buffer);
}

void CHandleProcess::VirtualServiceControl(HIOCPFILE hObject, PVOID Buffer, ULONG Size)
{
    ::ServiceControl_DispatchHandle(hObject, (PVIRTUAL_SERVICE_CONTROL)Buffer);
}